<? require("core/base.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>uptime - user stats</title>
<link href="files/main.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="800" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td><a href="index.php"><img src="img/hdr.png" alt="uptime" width="800" height="100" hspace="0" vspace="0" border="0"></a></td>
  </tr>  
  <tr>  
    <td valign="top" class="fons33"><?optionBar();?></td>
  </tr>
  <tr>  
    <td valign="top" class="fons33b">
      <table width="100%" border="0" cellspacing="0" cellpadding="8">
        <tr>
          <td><p><br>
              <span class="destacat"><br>
              <br>
			  <?
			  		$username=$_REQUEST['username'];
			  ?>
              User statistics for <I><? echo(webstr($username)); ?></I></span></p>
            <p><a href="#"></a><br>
            </p>
            <table width="600" border="0" cellspacing="1" cellpadding="1"> 
<?
	$bd=new bd;
	
	$sQuery="SELECT COUNT(*) AS res FROM users WHERE username=".dbstr($username);
	$bd->query($sQuery);
	
	if ($bd->getErrDescr()!="Ok")
	{
		echo("<TR>");
		echo("<TD width=\"170\" class=\"hdrtaula\">Database error</TD>");
		echo("<TD class=\"celltaula\">");		
		echo($bd->getErrDescr());		
		echo("</TD>");
		echo("</TR>");
	}
	else
	{
		$r=$bd->getResultatObj();
		if ($r[0]->res<=0)
		{
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Not found</TD>");
			echo("<TD class=\"celltaula\">");		
			echo("User ".webstr($username)." not found");		
			echo("</TD>");
			echo("</TR>");
	
		}
		else
		{	
			// 0wn3d machines	
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Owned machines</TD>");
			echo("<TD class=\"celltaula\">");
			
			$sQuery="SELECT name FROM machines WHERE username=".dbstr($username)." ORDER BY name";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
			
			if (count($r)==1)
				echo("one machine: <I>".$r[0]->name."</I>");
			else if (count($r)==0)
				echo("0 machines");
			else
			{
				echo(count($r)." machines: ");

				foreach ($r as $k => $v)
				{
					echo("<I>".$v->name."</I>");
					if ($k<=count($r)-3)
						echo(", ");
					else if ($k==count($r)-2)
						echo(" and ");
					else
						echo(".");
				}	
			}		
			echo("</TD>");
			echo("</TR>");
			
			// Has been a user for
			$sQuery="SELECT * FROM users WHERE username=".dbstr($username);
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Has been a user for</TD>");
			echo("<TD class=\"celltaula\">");		
			$tempsUsu=niceUptime(time()-dateToTimestamp($r[0]->subscribe_date));
			echo($tempsUsu);
			echo("</TD>");
			echo("</TR>");		
			
			
			// Real name
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Real name</TD>");
			echo("<TD class=\"celltaula\">");		
			echo($r[0]->realname);
			echo("</TD>");
			echo("</TR>");						

			// is admin?
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Is admin?</TD>");
			echo("<TD class=\"celltaula\">");
			if ($r[0]->admin=="t")
				echo("Yes");
			else
				echo("No");
			echo("</TD>");
			echo("</TR>");
			
			// avg uptime
			$sQuery="SELECT AVG(u.uptime) AS avguptime FROM uptime u WHERE u.username=".dbstr($username)." AND NOT EXISTS(SELECT reg_date FROM uptime u2 WHERE u2.username=u.username AND u2.machine=u.machine AND u2.reg_date>u.reg_date);";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Current avg confirmed uptime</TD>");
			echo("<TD class=\"celltaula\">");
			echo(niceUptime(round($r[0]->avguptime)));
			echo("</TD>");
			echo("</TR>");			


			// max uptime
			$sQuery="SELECT MAX(u.uptime) AS maxuptime FROM uptime u WHERE u.username=".dbstr($username)." AND NOT EXISTS(SELECT reg_date FROM uptime u2 WHERE u2.username=u.username AND u2.machine=u.machine AND u2.reg_date>u.reg_date);";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Current max confirmed uptime</TD>");
			echo("<TD class=\"celltaula\">");
			echo(niceUptime(round($r[0]->maxuptime)));
			echo("</TD>");
			echo("</TR>");
			

			// min uptime
			$sQuery="SELECT MIN(u.uptime) AS minuptime FROM uptime u WHERE u.username=".dbstr($username)." AND NOT EXISTS(SELECT reg_date FROM uptime u2 WHERE u2.username=u.username AND u2.machine=u.machine AND u2.reg_date>u.reg_date);";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Current min confirmed uptime</TD>");
			echo("<TD class=\"celltaula\">");
			echo(niceUptime(round($r[0]->minuptime)));
			echo("</TD>");
			echo("</TR>");


			// max uptime ever
			$sQuery="SELECT u.machine, u.reg_date, u.uptime FROM uptime u WHERE u.username=".dbstr($username)." ORDER BY u.uptime DESC LIMIT 1 OFFSET 0";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Max uptime ever</TD>");
			echo("<TD class=\"celltaula\">");
			echo(niceUptime(round($r[0]->uptime)).", confirmed by ".$r[0]->machine." on ".niceDate($r[0]->reg_date));
			echo("</TD>");
			echo("</TR>");


			// Min uptime ever
			$sQuery="SELECT u.machine, u.reg_date, u.uptime FROM uptime u WHERE u.username=".dbstr($username)." ORDER BY u.uptime LIMIT 1 OFFSET 0";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Min uptime ever</TD>");
			echo("<TD class=\"celltaula\">");
			echo(niceUptime(round($r[0]->uptime)).", confirmed by ".$r[0]->machine." on ".niceDate($r[0]->reg_date));
			echo("</TD>");
			echo("</TR>");
			
			
			// max/min deviations
			$sQuery="SELECT MAX(uptime_deviation) AS md FROM uptime WHERE username=".dbstr($username);
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
			
			$sQuery="SELECT MIN(uptime_deviation) AS md FROM uptime WHERE username=".dbstr($username);
			$bd->query($sQuery);
			$s=$bd->getResultatObj();
			

			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Max/min uptime deviations</TD>");
			echo("<TD class=\"celltaula\">");
			echo((int)$r[0]->md." / ".(int)$s[0]->md." seconds");
			echo("</TD>");
			echo("</TR>");
			
			
			// dominant OS
			$sQuery="SELECT s.os, COUNT(*) AS res FROM configurations c, subversions s WHERE c.username=".dbstr($username)." AND c.subversion=s.subversion GROUP BY s.os ORDER BY res DESC;";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Dominant OS</TD>");
			echo("<TD class=\"celltaula\">");
			
			$ant=-1;
			foreach ($r as $k => $v)
			{
				if ($v->res<$ant)
					break;
					
				echo($v->os." ");
				if ($v->res==1)
					echo("(one machine)");
				else
					echo("(".$v->res." machines)");
				echo("<BR>");
				
				$ant=$v->res;
			}
			echo("</TD>");
			echo("</TR>");



			// recessive OS
			$sQuery="SELECT s.os, COUNT(*) AS res FROM configurations c, subversions s WHERE c.username=".dbstr($username)." AND c.subversion=s.subversion GROUP BY s.os ORDER BY res;";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Recessive OS</TD>");
			echo("<TD class=\"celltaula\">");
			
			$ant=-1;
			foreach ($r as $k => $v)
			{
				if ($v->res>$ant && $k!=0)
					break;
					
				echo($v->os." ");
				if ($v->res==1)
					echo("(one machine)");
				else
					echo("(".$v->res." machines)");
				echo("<BR>");
				
				$ant=$v->res;
			}
			echo("</TD>");
			echo("</TR>");




			// dominant CPU
			$sQuery="SELECT c.cpu, COUNT(*) AS res FROM configurations c WHERE c.username=".dbstr($username)." GROUP BY c.cpu ORDER BY res DESC;";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Dominant CPU</TD>");
			echo("<TD class=\"celltaula\">");
			
			$ant=-1;
			foreach ($r as $k => $v)
			{
				if ($v->res<$ant)
					break;
					
				echo($v->cpu." ");
				if ($v->res==1)
					echo("(one machine)");
				else
					echo("(".$v->res." machines)");
				echo("<BR>");
				
				$ant=$v->res;
			}
			echo("</TD>");
			echo("</TR>");



			// recessive CPU
			$sQuery="SELECT c.cpu, COUNT(*) AS res FROM configurations c WHERE c.username=".dbstr($username)." GROUP BY c.cpu ORDER BY res;";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Recessive CPU</TD>");
			echo("<TD class=\"celltaula\">");
			
			$ant=-1;
			foreach ($r as $k => $v)
			{
				if (($v->res)>$ant && $k!=0)
					break;
					
				echo($v->cpu." ");
				if ($v->res==1)
					echo("(one machine)");
				else
					echo("(".$v->res." machines)");
				echo("<BR>");
				
				$ant=$v->res;
			}
			echo("</TD>");
			echo("</TR>");


			// uptime sum
			$sQuery="SELECT SUM(u.uptime) AS muptime FROM uptime u WHERE u.username=".dbstr($username);
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Total uptime</TD>");
			echo("<TD class=\"celltaula\">");
			echo(niceUptime(round($r[0]->muptime)));
			echo("</TD>");
			echo("</TR>");



			// different IPs
			$sQuery="SELECT u.ip, COUNT(*) AS res FROM uptime u WHERE u.username=".dbstr($username)." GROUP BY u.ip";
			$bd->query($sQuery);
			$r=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Number of different IPs used</TD>");
			echo("<TD class=\"celltaula\">");
			echo(count($r));
			echo("</TD>");
			echo("</TR>");



			// connections
			$sQuery="SELECT COUNT(*) AS res FROM uptime u WHERE u.username=".dbstr($username);
			$bd->query($sQuery);
			$r=$bd->getResultatObj();

			$sQuery="SELECT MIN(u.reg_date) AS m FROM uptime u WHERE u.username=".dbstr($username);
			$bd->query($sQuery);
			$s=$bd->getResultatObj();

			$sQuery="SELECT MAX(u.reg_date) AS m FROM uptime u WHERE u.username=".dbstr($username);
			$bd->query($sQuery);
			$t=$bd->getResultatObj();
		
			echo("<TR>");
			echo("<TD width=\"170\" class=\"hdrtaula\">Number of connections</TD>");
			echo("<TD class=\"celltaula\">");
			echo($r[0]->res.", spanning ".(niceUptime(dateToTimestamp($t[0]->m)-dateToTimestamp($s[0]->m))));
			echo("</TD>");
			echo("</TR>");
												
		}		
	}

?>			  
            </table>
            <p><br>
			
			
            <p><br>
              <strong>Machine configuration</strong>
			  
<?
		$sQuery="SELECT name FROM machines WHERE username=".dbstr($username);
		$bd->query($sQuery);
		$r=$bd->getResultatObj();
		
		foreach ($r as $k => $v)
		{
?>			  
			  <br>
              <br>
              <? echo($v->name); ?></p>
			  
            <table width="780" border="0" cellspacing="1" cellpadding="1">
              <tr> 
                <td width="20%" class="hdrtaula">From</td>
                <td width="21%" class="hdrtaula">To</td>
                <td width="37%" class="hdrtaula">OS</td>
                <td width="22%" class="hdrtaula">Hardware</td>
              </tr>
<?			  
			$sQuery="SELECT c.ini_date, c.fin_date, s.os, s.version, s.info, c.cpu FROM configurations c, subversions s WHERE c.username=".dbstr($username)." AND c.machine=".dbstr($v->name)." AND s.subversion=c.subversion";
			$bd->query($sQuery);
	
			$s=$bd->getResultatObj();

			foreach ($s as $k2 => $v2)
			{
				echo("<TR>");
				echo("<TD class=\"celltaulafirst\">".niceDate($v2->ini_date)."</TD>");
				
				if ($v2->fin_date==NULL)
					echo("<TD class=\"celltaula\">-</TD>");				
				else
					echo("<TD class=\"celltaula\">".niceDate($v2->fin_date)."</TD>");
				echo("<TD class=\"celltaula\">".$v2->os." ".$v2->version." ".$v2->info."</TD>");
				echo("<TD class=\"celltaula\">".$v2->cpu."</TD>");
				echo("</TR>");
			}
?>			  
            </table>
<?
		}
?>			
			
            <p> <br>
			
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
            </p>
	<DIV align="right">
      <a href="http://validator.w3.org/check?uri=referer"><img border="0"
          src="http://www.w3.org/Icons/valid-html401"
          alt="Valid HTML 4.01!" height="31" width="88"></a>
    </DIV>			
            <p>&nbsp;</p></td>
        </tr>
      </table>
      
    </td>
  </tr>
</table>
</body>
</html>
